﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace LocalizationTest
{
  public partial class ExperimentConfigurator : UserControl
  {
    Experiment experiment;
    bool comboBoxesInitialized = false;

    public ExperimentConfigurator()
    {
      InitializeComponent();
    }

    public void Init(Experiment experiment)
    {
      this.experiment = experiment;
      if(!comboBoxesInitialized) InitComboBoxes();
      else UpdateComboboxes();
    }

    private void InitComboBoxes()
    {
      FillComboBox(typeof(Map), comboBoxMapSelector);
      FillComboBox(typeof(MotionModel), comboBoxMotionModelSelector);
      FillComboBox(typeof(WorldState), comboBoxWorldStateSelector);
      FillComboBox(typeof(ActionDescription), comboBoxActionSelector);
      
      UpdateComboboxes();
      
      comboBoxMapSelector.SelectedValueChanged += new EventHandler(comboBoxMapSelector_SelectedValueChanged);
      comboBoxMotionModelSelector.SelectedValueChanged += new EventHandler(comboBoxMotionModelSelector_SelectedValueChanged);
      comboBoxWorldStateSelector.SelectedValueChanged += new EventHandler(comboBoxWorldStateSelector_SelectedValueChanged);
      comboBoxActionSelector.SelectedValueChanged += new EventHandler(comboBoxActionSelector_SelectedValueChanged);

      comboBoxesInitialized = true;
    }

    private void UpdateComboboxes()
    {
      comboBoxMapSelector.Text = experiment.map.GetType().Name;
      comboBoxMotionModelSelector.Text = experiment.motionModel.GetType().Name;
      comboBoxWorldStateSelector.Text = experiment.simulatedWorldState.GetType().Name;
      comboBoxActionSelector.Text = experiment.typeOfActionDescription.Name;
    }

    void comboBoxMapSelector_SelectedValueChanged(object sender, EventArgs e)
    {
      Type type = Type.GetType(this.GetType().Namespace + "." + comboBoxMapSelector.Text);
      experiment.map = (Map)System.Activator.CreateInstance(type);
      ConfigurationChanged(this, null);
    }
    void comboBoxMotionModelSelector_SelectedValueChanged(object sender, EventArgs e)
    {
      Type type = Type.GetType(this.GetType().Namespace + "." + comboBoxMotionModelSelector.Text);
      experiment.motionModel = (MotionModel)System.Activator.CreateInstance(type);
      ConfigurationChanged(this, null);
    }
    void comboBoxWorldStateSelector_SelectedValueChanged(object sender, EventArgs e)
    {
      Type type = Type.GetType(this.GetType().Namespace + "." + comboBoxWorldStateSelector.Text);
      experiment.simulatedWorldState = (WorldState)System.Activator.CreateInstance(type);
      ConfigurationChanged(this, null);
    }
    void comboBoxActionSelector_SelectedValueChanged(object sender, EventArgs e)
    {
      Type type = Type.GetType(this.GetType().Namespace + "." + comboBoxActionSelector.Text);
      experiment.typeOfActionDescription = type;
      ConfigurationChanged(this, null);
    }

    private void FillComboBox(Type typeOfEntries, ComboBox comboBoxToFill)
    {
      Assembly assembly = Assembly.GetEntryAssembly();
      foreach (Module module in assembly.GetModules())
      {
        foreach (Type type in module.GetTypes())
        {
          if (type.IsSubclassOf(typeOfEntries))
          {
            comboBoxToFill.Items.Add(type.Name);
          }
        }
      }
      comboBoxToFill.Sorted = true;
    }

    public event EventHandler ConfigurationChanged;

  }
}
